﻿/*
* Copyright (c) 2010-2012 Tesla Engine Group
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/


using System;
using System.Globalization;
using System.Runtime.InteropServices;

namespace Tesla.Math {
    /// <summary>
    /// Defines a 2D point.
    /// </summary>
    [Serializable]
    [StructLayout(LayoutKind.Sequential)]
    public struct Point : IEquatable<Point> {
        /// <summary>
        /// X coordinate
        /// </summary>
        public int X;

        /// <summary>
        /// Y coordinate
        /// </summary>
        public int Y;

        private static Point _zero = new Point();

        /// <summary>
        /// Gets the point at the origin.
        /// </summary>
        public static Point Zero {
            get {
                return _zero;
            }
        }

        /// <summary>
        /// Creates a new Point
        /// </summary>
        /// <param name="x">X coordinate</param>
        /// <param name="y">Y coordinate</param>
        public Point(int x, int y) {
            this.X = x;
            this.Y = y;
        }

        /// <summary>
        /// Tests equality between this point and another.
        /// </summary>
        /// <param name="other">Point to test</param>
        /// <returns>True if equal</returns>
        public bool Equals(Point other) {
            return (this.X == other.X) && (this.Y == other.Y);
        }

        /// <summary>
        /// Tests equality between this point and the supplied object.
        /// </summary>
        /// <param name="obj">Object to test</param>
        /// <returns>True if equal</returns>
        public override bool Equals(Object obj) {
            if(obj is Point){
                return Equals((Point) obj);
            }
            return false;
        }

        /// <summary>
        /// Get the hashcode of this point.
        /// </summary>
        /// <returns>Hash code</returns>
        public override int GetHashCode() {
            return this.X.GetHashCode() + this.Y.GetHashCode();
        }

        /// <summary>
        /// Get the string representation of this point.
        /// </summary>
        /// <returns>String representation</returns>
        public override String ToString() {
            CultureInfo info = CultureInfo.CurrentCulture;
            return String.Format(info, "{{X:{0} Y:{1}}}",
                new Object[] { this.X.ToString(info), this.Y.ToString(info) });
        }

        /// <summary>
        /// Tests equality between two points.
        /// </summary>
        /// <param name="a">First point</param>
        /// <param name="b">Second point</param>
        /// <returns>True if equal</returns>
        public static bool operator ==(Point a, Point b){
            return (a.X == b.X) && (a.Y == b.Y);
        }

        /// <summary>
        /// Tests inequality between two points.
        /// </summary>
        /// <param name="a">First point</param>
        /// <param name="b">Second point</param>
        /// <returns>True if not equal</returns>
        public static bool operator !=(Point a, Point b){
            return !(a == b);
        }
    }
}
